package com.example.hello_word.controller;


import com.example.hello_word.bean.User;
import com.example.hello_word.descImg.DescImg;
import com.example.hello_word.descImg.DescImgService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
public class DescImgController {

    @Autowired
    DescImgService service;

    @RequestMapping("/getDescImgList")
    @ResponseBody
    public List<DescImg> getDescImgList() {
        List<DescImg> allInfo = service.findAll();
        return allInfo;
    }

    @RequestMapping(value = "/saveDescImg", method = RequestMethod.POST)
    @ResponseBody
    public String saveDescImg(@RequestBody DescImg descImg) {
        try {
            service.save(descImg);
            return "成功";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "失败";
        }
    }

    @RequestMapping(value = "/saveImg",method = RequestMethod.POST)
    public String saveImg(@RequestParam MultipartFile params, Integer id) {
        String path = "D://mysql_img/uploads/";
        String fileType = params.getOriginalFilename().substring(params.getOriginalFilename().indexOf("."));
        String fileName = UUID.randomUUID().toString().replace("-", "");
        DescImg serviceById = service.findById(id);
        String fileEndPath = path + fileName + fileType;
        serviceById.setImgUrl(fileEndPath);
        //保存路劲到数据库
        service.save(serviceById);
        //保存文件到本地(数据库或者硬盘上面)
        try {
            params.transferTo(new File(fileEndPath));
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return fileEndPath;
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public DescImg getInfoById(Integer id) {
        return service.findById(id);
    }

    @RequestMapping(value = "/getPic", method = RequestMethod.POST, produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity getPic(Integer id) {
        try {
            String imgUrl = service.findById(id).getImgUrl();

            System.out.println(imgUrl);
            FileInputStream stream = new FileInputStream(imgUrl);
            byte[] byteArray = getByteArray(stream);

            HttpHeaders headers = new HttpHeaders();
            String fileType = imgUrl.substring(imgUrl.lastIndexOf(".") + 1);
            System.out.println(fileType);
            if (fileType == "png" || fileType == "PNG") {
                headers.setContentType(MediaType.IMAGE_PNG);
            }else if (fileType == "gif") {
                headers.setContentType(MediaType.IMAGE_GIF);
            } else {
                headers.setContentType(MediaType.IMAGE_JPEG);
            }
            return new ResponseEntity<>(byteArray, headers, HttpStatus.OK);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public byte[] getByteArray(FileInputStream inputStream) {
        try {
            int b;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            byte[] byteTemp = new byte[1024];
            while ((b = inputStream.read(byteTemp)) != -1) {
                byteArrayOutputStream.write(byteTemp, 0, b);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[]{};
        }

    }
}
